package com.ge.modules.sys.controller;

import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.entity.TbUser;
import com.ge.modules.sys.service.SysTbUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-08-07 14:42:15
 */
@RestController
@RequestMapping("sys/tbUser")
public class SysTbUserController {
    @Autowired
    private SysTbUserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("tbUser:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TbUser> userList = userService.queryList(query);
        int total = userService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("tbUser:info")
    public R info(@PathVariable("userId") Integer userId) {
        TbUser user = userService.queryObject(userId);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("tbUser:save")
    public R save(@RequestBody TbUser user) {
        user.setDeleteType(0);
        user.setCreateTime(new Date());
        user.setIsTechnician(0);
        user.setUserType(1);
        user.setIntegral(0);
        user.setWallet(BigDecimal.valueOf(0));
        userService.save(user);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("tbUser:update")
    public R update(@RequestBody TbUser user) {
        userService.update(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("tbUser:delete")
    public R delete(@RequestBody Integer[] userIds) {
        userService.deleteBatch(userIds);

        return R.ok();
    }

}
